package com.example.usercenter.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.usercenter.common.BaseResponse;
import com.example.usercenter.common.ErrorCode;
import com.example.usercenter.common.ResultUtils;
import com.example.usercenter.exception.BussinessException;
import com.example.usercenter.model.domain.User;
import com.example.usercenter.service.UserService;
import com.example.usercenter.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service


public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired

    private UserMapper userMapper;

    @Override

    public long userRegister(String userAccount, String userPassword, String checkPassword) {



        //判断账户,用户名,确认密码不为空
        if(StringUtils.isAnyBlank(userAccount,userPassword,checkPassword)){

            throw new BussinessException("账户/密码/确认密码不得为空!", ErrorCode.SYSTEM_ERROR.getCode(),"");
        }

        //账户的长度
        if(userAccount.length()<4){

            throw new BussinessException("账户的长度小于4", ErrorCode.SYSTEM_ERROR.getCode(),"");
        }

        //密码-确认密码长度不得小于8位

        if(userPassword.length()<8 ||checkPassword.length()<8 ){

            throw new BussinessException("账户的长度小于8", ErrorCode.SYSTEM_ERROR.getCode(),"");
        }

        //账户账户不得有特殊字符
        String validPattern="\\w+$";
        Matcher matcher= Pattern.compile(validPattern).matcher(userAccount);
        if(!((Matcher) matcher).find()){
            throw new BussinessException("账户账户不得有特殊字符", ErrorCode.SYSTEM_ERROR.getCode(),"");

        }

        //两次输入密码不一致

        if(!userPassword.equals(checkPassword)){

            throw new BussinessException("两次输入密码不一致", ErrorCode.SYSTEM_ERROR.getCode(),"");
        }

        //账户不得重复

        QueryWrapper queryWrapper=new QueryWrapper<>();

        queryWrapper.eq("userAccount",userAccount);

        long count = this.count(queryWrapper);

        if(count>0){

            throw new BussinessException("当前账户已注册!", ErrorCode.SYSTEM_ERROR.getCode(),"");
        }

        //加密密码

        final  String SALT="passWord";

        String encryptPassword= DigestUtils.md5DigestAsHex((SALT+userPassword).getBytes());

        User  user =new User();

        user.setUserAccount(userAccount);

        user.setUserPassword(encryptPassword);

        boolean  saveResult=this.save(user);

        if(!saveResult){
            throw new BussinessException("新增失败!", ErrorCode.SYSTEM_ERROR.getCode(),"");
        }

        return  user.getId();

    }


    @Override
    public User userLogin(String userAccount, String userPassword, HttpServletRequest request) {

        //判断账户,用户名,不为空
        if (StringUtils.isAnyBlank(userAccount,userPassword)){
            System.out.println("账户/密码不得为空!");
            return null;
        }

        //账户长度不小于4
        if (userAccount.length()<4){
            return null;

        }

        //密码不小于8
        if (userPassword.length()<8){
            return null;
        }

        //账户不得含有特殊字符

        String validPattern="\\w+$";
        Matcher matcher= Pattern.compile(validPattern).matcher(userAccount);
        if(!((Matcher) matcher).find()){
            System.out.println("特殊字符");
            return null;
        }

        //加密,检查用户是否存在

        final  String SALT="passWord";

        String encryptPassword= DigestUtils.md5DigestAsHex((SALT+userPassword).getBytes());

        QueryWrapper  queryWrapper =new QueryWrapper<>();

        queryWrapper.eq("userAccount",userAccount);

        queryWrapper.eq("userPassword",encryptPassword);

        User user=userMapper.selectOne(queryWrapper);

        if(user==null){
            return   null;
        }

        //返回用户信息(脱敏)

        User safeUserInfo=new User();
        safeUserInfo.setId(user.getId());
        safeUserInfo.setUsername(user.getUsername());
        safeUserInfo.setGender(user.getGender());
        safeUserInfo.setUserAccount(user.getUserAccount());
        safeUserInfo.setAvatarUrl(user.getAvatarUrl());
        safeUserInfo.setPhone(user.getPhone());
        safeUserInfo.setUserStatus(user.getUserStatus());

        //记录登录态(session)
        request.getSession().setAttribute("session", safeUserInfo);

        return safeUserInfo;
    }

    @Override
    public int userLayout(HttpServletRequest request) {

        HttpSession session=request.getSession(false);

       //判断是否为null
        if(session!=null){
            // 清除session中的用户信息
            session.removeAttribute("session");
            // 使当前会话失效
            session.invalidate();
        }
         System.out.println("移除成功!");
          return 0;
    }

}





